<?php

namespace common\models\datas;

use common\components\extenders\MenuManage;
use frontend\models\datas\Mechanic;
use Yii;

/**
 * This is the model class for table "{{%sales_mechanic}}".
 *
 * @property integer $id
 * @property integer $mechanic_id
 * @property string $telephone
 * @property string $address
 * @property string $relation
 * @property string $salesman_tel
 * @property integer $created_at
 * @property integer $user_id
 * @property integer $status
 * @property integer $updated_at
 * @property string $remark
 */
class SalesMechanic extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sales_mechanic}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mechanic_id', 'created_at', 'user_id', 'status', 'updated_at','site_id','customer_id','is_contacts','type'], 'integer'],
            [['telephone', 'relation','contacts'], 'string', 'max' => 50],
            [['reason'], 'string'],
            [['address','mechanic_name'], 'string', 'max' => 200],
            [['remark'],'remark_required','skipOnEmpty'=>false,'on' => ['update']],
            [['mechanic_name','address','telephone','contacts'],'self_required','skipOnEmpty'=>false,'on' => ['recommend_self']],
            [['mechanic_name','address'],'self_required','skipOnEmpty'=>false,'on' => ['recommend_mechanic']],
            ['telephone', 'match', 'pattern' => '/^1([0-9]{9})/', 'message' => '手机号码格式错误'],
            [['contacts'],'check_contacts','skipOnEmpty'=>false,'on' => ['recommend_mechanic']],
            [['telephone'],'check_telephone','skipOnEmpty'=>false,'on' => ['recommend_mechanic']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'id'),
            'mechanic_id' => Yii::t('common', '机构id'),
            'telephone' => Yii::t('common', '机构联系人电话'),
            'address' => Yii::t('common', '机构地址'),
            'relation' => Yii::t('common', '与联系人关系'),
            'customer_id' => Yii::t('common', '会员id'),
            'created_at' => Yii::t('common', '创建时间'),
            'user_id' => Yii::t('common', '处理人id'),
            'status' => Yii::t('common', '状态'),
            'updated_at' => Yii::t('common', '最后处理时间'),
            'remark' => Yii::t('common', '备注'),
            'contacts' => Yii::t('common', '联系人'),
            'mechanic_name' => Yii::t('common', '机构名称'),
            'is_contacts' => Yii::t('common', '是否有联系人'),
            'type' => Yii::t('common', '推荐类型'),
            'reason' => Yii::t('common', '推荐理由'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            if($this->updated_at >0)
                return \Yii::$app->formatter->asDatetime($this->updated_at);
            else
                return Yii::t('frontend','未处理');
        };

        $fields['status'] = function(){
            switch($this->status){
                case 0:
                    return Yii::t('common', '已提交');
                case 1:
                    return Yii::t('common', '处理中');
                case 2:
                    return Yii::t('common', '加盟成功');
                case 3:
                    return Yii::t('common', '加盟失败');
            }
        };
        $fields['type'] = function(){
            switch($this->type){
                case 0:
                    return Yii::t('common','自我推荐');
                case 1:
                    return Yii::t('common','推荐其他机构');
            }
        };
        $fields['href'] = function () {
            return \Yii::$app->params['wechat_url'].'/recommendmechanic/list';
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'customer_name' => function(){
                return isset($this->customer)?$this->customer->name:'';
            },
            'mobile' => function(){
                return isset($this->customer)?$this->customer->mobile:'';
            },
            'user_name' =>function(){
                return isset($this->user)?$this->user->name:'未处理';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function remark_required($attribute){
        if($this->status > 0 && $this->$attribute == ''){
            $this->addError($attribute, Yii::t('frontend', '处理备注不能为空'));
        }
    }

    public function getMechanic(){
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanic_id']);
    }

    public function getUser(){
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    public function self_required($attributes){
        if(empty($this->$attributes)){
            $this->addError($attributes,'信息不能为空');
        }
    }

    public function check_contacts($attributes){
        if($this->is_contacts==1&&empty($this->$attributes)){
            $this->addError($attributes,'联系人信息不能为空');
        }
    }
    public function check_telephone($attributes){
        if($this->is_contacts==1&&empty($this->$attributes)){
            $this->addError($attributes,'联系人信息不能为空');
        }
    }
}
